package net.mihome.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.mihome.app.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ZHAO on 2014/12/21 0021.
 */
public class MihomeContractAdapter  extends BaseAdapter {
    private List<String> contractList=new ArrayList<>();
    private Context context;
    private LayoutInflater listContainer;//视图容器
    static class ListItemView{				//自定义控件集合
        public TextView nameTextView;
        public ImageView avatarImageView;
    }

    public MihomeContractAdapter(Context context, List<String> contractList) {
        this.contractList = contractList;
        this.context=context;
        this.listContainer = LayoutInflater.from(context);	//创建视图容器并设置上下文

    }

    @Override
    public int getCount() {
        return contractList.size();
    }

    @Override
    public Object getItem(int i) {
        return contractList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ListItemView listItemView=null;
        if(view==null){
            listItemView=new ListItemView();
            view = listContainer.inflate(R.layout.row_contact, null);
            listItemView=new ListItemView();
            listItemView.nameTextView= (TextView) view.findViewById(R.id.name);
            listItemView.avatarImageView= (ImageView) view.findViewById(R.id.avatar);
            view.setTag(listItemView);
        }else {
            listItemView = (ListItemView)view.getTag();
        }
        String name=contractList.get(i);
        listItemView.nameTextView.setText(name);
        if(i==0){
            //如果是第一个，是添加好友，所以放默认图标
            listItemView.avatarImageView.setImageResource(R.drawable.smiley_add_btn_nor);
        }
        return view;
    }
}
